<?php namespace App\Http\Services;

class ToolsManager
{
    /**
     * 生成订单编号
     * @return string
     */
    public static function makeBillNum(){
        $orderSn = date('Ymdhis',time()). substr(microtime(), 2, 5) . rand(100, 999);
        return $orderSn;
    }

    /**
     * 生城提取码
     * @return string
     */
    public static function makePickupCode(){
        $pickupCode = rand(100000,999999);
        return $pickupCode;
    }

    /**
     * 生成退款单号
     * @return string
     */
    public static function makeBRefundNum(){
        $orderSn = date('Ymdhis',time()). substr(microtime(), 2, 5) . rand(100, 999) . '@tui';
        return $orderSn;
    }

    /**
     * 求两个已知经纬度之间的距离,单位为米
     *
     * @param $lng1 ,$lng2 经度
     * @param $lat1 ,$lat2 纬度
     * @param $lng2
     * @param $lat2
     * @return float 距离，单位米
     */
    public static function getDistance($lng1, $lat1, $lng2, $lat2) {
        // 将角度转为狐度
        if ($lat1=='' || $lng1=='' || $lat2=='' || $lng2=='')
            return 0;
        $radLat1 = deg2rad($lat1); //deg2rad()函数将角度转换为弧度
        $radLat2 = deg2rad($lat2);
        $radLng1 = deg2rad($lng1);
        $radLng2 = deg2rad($lng2);
        $a = $radLat1 - $radLat2;
        $b = $radLng1 - $radLng2;
        $s = 2 * asin(sqrt(pow(sin($a / 2), 2) + cos($radLat1) * cos($radLat2) * pow(sin($b / 2), 2))) * 6378.137 * 1000;
        if ($s==0){
            $s=1;
        }
        $dis=round($s,2);
//        if ($dis>999){
            $dis=round($dis/1000,2).'km';
//        }else{
//            $dis=$dis.'m';
//        }
        return $dis;
    }

    /**
     * 获取指定方圆距离的四个点
     * param lng float 经度
     * param lat float 纬度
     * param distance float 该点所在圆的半径，该圆与此正方形内切，默认值为单位米
     * return array 正方形的四个点的经纬度坐标
     * @param $lng
     * @param $lat
     * @param $distance
     * @return array
     */
    public static function getSquarePoint($lng, $lat,$distance)
    {
        $PI = 3.14159265;
        $longitude = $lng;
        $latitude = $lat;

        $degree = (24901*1609)/360.0;
        $raidusMile = $distance;

        $dpmLat = 1/$degree;
        $radiusLat = $dpmLat*$raidusMile;
        $minLat = $latitude - $radiusLat;       //拿到最小纬度
        $maxLat = $latitude + $radiusLat;       //拿到最大纬度

        $mpdLng = $degree*cos($latitude * ($PI/180));
        $dpmLng = 1 / $mpdLng;
        $radiusLng = $dpmLng*$raidusMile;
        $minLng = $longitude - $radiusLng;     //拿到最小经度
        $maxLng = $longitude + $radiusLng;     //拿到最大经度
        $range = array(
            'minLat' => $minLat,
            'maxLat' => $maxLat,
            'minLng' => $minLng,
            'maxLng' => $maxLng
        );
        return $range;
    }

    public static function coverString($string){
        if ($string=='')
            return '';
        $string =trim($string);
        $first4 =mb_substr($string,0,4,'utf8');
        $end2   =mb_substr($string,-2,2,'utf8');
        $result =$first4.'*'.$end2;
        return $result;
    }

    /**
     * 把图片转换成圆角
     * @param string $imgpath
     * @param int $radius
     * @return resource
     */
    public function radius_img_url($imgpath = './t.png', $radius = 15) {
        $ext     = pathinfo($imgpath);
        $src_img = null;
        switch ($ext['extension']) {
            case 'jpg':
                $src_img = imagecreatefromjpeg($imgpath);
                break;
            case 'png':
                $src_img = imagecreatefrompng($imgpath);
                break;
        }
        $wh = getimagesize($imgpath);
        $w  = $wh[0];
        $h  = $wh[1];
        return $this->radius_img($src_img, $w,$h, $radius);
    }
    public function radius_img($src_img, $width,$height, $radius = 15) {

        $w  = &$width;
        $h  = &$height;
        // $radius = $radius == 0 ? (min($w, $h) / 2) : $radius;
        $img = imagecreatetruecolor($w, $h);
        //这一句一定要有
        imagesavealpha($img, true);
        //拾取一个完全透明的颜色,最后一个参数127为全透明
        $bg = imagecolorallocatealpha($img, 255, 255, 255, 127);
        imagefill($img, 0, 0, $bg);
        $r = $radius; //圆 角半径
        for ($x = 0; $x < $w; $x++) {
            for ($y = 0; $y < $h; $y++) {
                $rgbColor = imagecolorat($src_img, $x, $y);
                if (($x >= $radius && $x <= ($w - $radius)) || ($y >= $radius)) {
//                    //不在四角的范围内,直接画
                    imagesetpixel($img, $x, $y, $rgbColor);
                } else {
                    //在四角的范围内选择画
                    //上左
                    $y_x = $r; //圆心X坐标
                    $y_y = $r; //圆心Y坐标
                    if (((($x - $y_x) * ($x - $y_x) + ($y - $y_y) * ($y - $y_y)) <= ($r * $r))) {
                        imagesetpixel($img, $x, $y, $rgbColor);
                    }
                    //上右
                    $y_x = $w - $r; //圆心X坐标
                    $y_y = $r; //圆心Y坐标
                    if (((($x - $y_x) * ($x - $y_x) + ($y - $y_y) * ($y - $y_y)) <= ($r * $r))) {
                        imagesetpixel($img, $x, $y, $rgbColor);
                    }
                    //下左
                    $y_x = $r; //圆心X坐标
                    $y_y = $h - $r; //圆心Y坐标
                    if (((($x - $y_x) * ($x - $y_x) + ($y - $y_y) * ($y - $y_y)) <= ($r * $r))) {
                        imagesetpixel($img, $x, $y, $rgbColor);
                    }
                    //下右
                    $y_x = $w - $r; //圆心X坐标
                    $y_y = $h - $r; //圆心Y坐标
                    if (((($x - $y_x) * ($x - $y_x) + ($y - $y_y) * ($y - $y_y)) <= ($r * $r))) {
                        imagesetpixel($img, $x, $y, $rgbColor);
                    }
                }
            }
        }
        return $img;
    }
}
